using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using TmxLib;

namespace TmxTestNsi
{
    class Program
    {
        static void Main(string[] args)
        {
            Args a = ParseArgs(args);
            if (a == null)
            {
                Console.WriteLine(
                    "Usage: {0} /EditorPath:<editor path> /File:<Tmx File>",
                    Assembly.GetExecutingAssembly().GetName().Name + ".exe"
                    );

                return;
            }

            Console.WriteLine("Create Wizard to edit " + a.File);
            // NsiProjectBuilder b = new NsiProjectBuilder(@"C:\man\TmxEditor\TmxEditor\bin\Debug", @"C:\work\program\biotime\REL_3.6.0\SOURCE\BioTime\BioTime.tmx");
            NsiProjectBuilder b = new NsiProjectBuilder(a.EditorPath, a.File);
            NsiProjectBuilder.Result result;
            using (b)
            {
                result = b.Build("BioTime Resources (Ukrainian)", "Snapshot " + DateTime.Now.Date.ToString("yyyy.MM.dd"));
            }

            using(FileStream fs = new FileStream(result.AppName + ".exe", FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                fs.Write(result.ExecutableImage, 0, result.ExecutableImage.Length);
            }
        }

        static Args ParseArgs(string[] args)
        {
            Args ret = new Args();
            foreach(string a in args)
            {
                if (a.StartsWith("/"))
                {
                    int p = a.IndexOf(':');
                    if (p > 0)
                    {
                        string pName = a.Substring(1, p - 1);
                        string pValue = a.Substring(p + 1);
                        
                        if (string.Compare(pName, "editorpath", true) == 0)
                        {
                            ret.EditorPath = pValue;
                        }

                        if (string.Compare(pName, "file", true) == 0)
                        {
                            ret.File = pValue;
                        }
                    }
                }
            }

            if (ret.EditorPath == null || ret.File == null)
                return null;

            return ret;
        }
    }

    class Args
    {
        public string EditorPath;
        public string File;
    }
}
